home *** CD-ROM | disk | FTP | other *** search
/ Aminet 8 / Aminet 8 (1995)(GTI - Schatztruhe)[!][Oct 1995].iso / Aminet / dev / gcc / gcc270_src.lha / gcc-2.7.0-amiga / explow.c < prev    next >
C/C++ Source or Header  |  1995-08-24  |  34KB  |  1,229 lines

  1. /* Subroutines for manipulating rtx's in semantically interesting ways.
  2.    Copyright (C) 1987, 1991, 1994, 1995 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU CC.
  5.  
  6. GNU CC is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GNU CC is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU CC; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 59 Temple Place - Suite 330,
  19. Boston, MA 02111-1307, USA.  */
  20.  
  21.  
  22. #include "config.h"
  23. #include "rtl.h"
  24. #include "tree.h"
  25. #include "flags.h"
  26. #include "expr.h"
  27. #include "hard-reg-set.h"
  28. #include "insn-config.h"
  29. #include "recog.h"
  30. #include "insn-flags.h"
  31. #include "insn-codes.h"
  32.  
  33. rtx gen_stack_cleanup_call();
  34. static rtx break_out_memory_refs    PROTO((rtx));
  35.  
  36. /* Return an rtx for the sum of X and the integer C.
  37.  
  38.    This function should be used via the `plus_constant' macro.  */
  39.  
  40. rtx
  41. plus_constant_wide (x, c)
  42.      register rtx x;
  43.      register HOST_WIDE_INT c;
  44. {
  45.   register RTX_CODE code;
  46.   register enum machine_mode mode;
  47.   register rtx tem;
  48.   int all_constant = 0;
  49.  
  50.   if (c == 0)
  51.     return x;
  52.  
  53.  restart:
  54.  
  55.   code = GET_CODE (x);
  56.   mode = GET_MODE (x);
  57.   switch (code)
  58.     {
  59.     case CONST_INT:
  60.       return GEN_INT (INTVAL (x) + c);
  61.  
  62.     case CONST_DOUBLE:
  63.       {
  64.     HOST_WIDE_INT l1 = CONST_DOUBLE_LOW (x);
  65.     HOST_WIDE_INT h1 = CONST_DOUBLE_HIGH (x);
  66.     HOST_WIDE_INT l2 = c;
  67.     HOST_WIDE_INT h2 = c < 0 ? ~0 : 0;
  68.     HOST_WIDE_INT lv, hv;
  69.  
  70.     add_double (l1, h1, l2, h2, &lv, &hv);
  71.  
  72.     return immed_double_const (lv, hv, VOIDmode);
  73.       }
  74.  
  75.     case MEM:
  76.       /* If this is a reference to the constant pool, try replacing it with
  77.      a reference to a new constant.  If the resulting address isn't
  78.      valid, don't return it because we have no way to validize it.  */
  79.       if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
  80.       && CONSTANT_POOL_ADDRESS_P (XEXP (x, 0)))
  81.     {
  82.       tem
  83.         = force_const_mem (GET_MODE (x),
  84.                    plus_constant (get_pool_constant (XEXP (x, 0)),
  85.                           c));
  86.       if (memory_address_p (GET_MODE (tem), XEXP (tem, 0)))
  87.         return tem;
  88.     }
  89.       break;
  90.  
  91.     case CONST:
  92.       /* If adding to something entirely constant, set a flag
  93.      so that we can add a CONST around the result.  */
  94.       x = XEXP (x, 0);
  95.       all_constant = 1;
  96.       goto restart;
  97.  
  98.     case SYMBOL_REF:
  99.     case LABEL_REF:
  100.       all_constant = 1;
  101.       break;
  102.  
  103.     case PLUS:
  104.       /* The interesting case is adding the integer to a sum.
  105.      Look for constant term in the sum and combine
  106.      with C.  For an integer constant term, we make a combined
  107.      integer.  For a constant term that is not an explicit integer,
  108.      we cannot really combine, but group them together anyway.  
  109.  
  110.      Use a recursive call in case the remaining operand is something
  111.      that we handle specially, such as a SYMBOL_REF.  */
  112.  
  113.       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
  114.     return plus_constant (XEXP (x, 0), c + INTVAL (XEXP (x, 1)));
  115.       else if (CONSTANT_P (XEXP (x, 0)))
  116.     return gen_rtx (PLUS, mode,
  117.             plus_constant (XEXP (x, 0), c),
  118.             XEXP (x, 1));
  119.       else if (CONSTANT_P (XEXP (x, 1)))
  120.     return gen_rtx (PLUS, mode,
  121.             XEXP (x, 0),
  122.             plus_constant (XEXP (x, 1), c));
  123.     }
  124.  
  125.   if (c != 0)
  126.     x = gen_rtx (PLUS, mode, x, GEN_INT (c));
  127.  
  128.   if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF)
  129.     return x;
  130.   else if (all_constant)
  131.     return gen_rtx (CONST, mode, x);
  132.   else
  133.     return x;
  134. }
  135.  
  136. /* This is the same as `plus_constant', except that it handles LO_SUM.
  137.  
  138.    This function should be used via the `plus_constant_for_output' macro.  */
  139.  
  140. rtx
  141. plus_constant_for_output_wide (x, c)
  142.      register rtx x;
  143.      register HOST_WIDE_INT c;
  144. {
  145.   register RTX_CODE code = GET_CODE (x);
  146.   register enum machine_mode mode = GET_MODE (x);
  147.   int all_constant = 0;
  148.  
  149.   if (GET_CODE (x) == LO_SUM)
  150.     return gen_rtx (LO_SUM, mode, XEXP (x, 0),
  151.             plus_constant_for_output (XEXP (x, 1), c));
  152.  
  153.   else
  154.     return plus_constant (x, c);
  155. }
  156.  
  157. /* If X is a sum, return a new sum like X but lacking any constant terms.
  158.    Add all the removed constant terms into *CONSTPTR.
  159.    X itself is not altered.  The result != X if and only if
  160.    it is not isomorphic to X.  */
  161.  
  162. rtx
  163. eliminate_constant_term (x, constptr)
  164.      rtx x;
  165.      rtx *constptr;
  166. {
  167.   register rtx x0, x1;
  168.   rtx tem;
  169.  
  170.   if (GET_CODE (x) != PLUS)
  171.     return x;
  172.  
  173.   /* First handle constants appearing at this level explicitly.  */
  174.   if (GET_CODE (XEXP (x, 1)) == CONST_INT
  175.       && 0 != (tem = simplify_binary_operation (PLUS, GET_MODE (x), *constptr,
  176.                         XEXP (x, 1)))
  177.       && GET_CODE (tem) == CONST_INT)
  178.     {
  179.       *constptr = tem;
  180.       return eliminate_constant_term (XEXP (x, 0), constptr);
  181.     }
  182.  
  183.   tem = const0_rtx;
  184.   x0 = eliminate_constant_term (XEXP (x, 0), &tem);
  185.   x1 = eliminate_constant_term (XEXP (x, 1), &tem);
  186.   if ((x1 != XEXP (x, 1) || x0 != XEXP (x, 0))
  187.       && 0 != (tem = simplify_binary_operation (PLUS, GET_MODE (x),
  188.                         *constptr, tem))
  189.       && GET_CODE (tem) == CONST_INT)
  190.     {
  191.       *constptr = tem;
  192.       return gen_rtx (PLUS, GET_MODE (x), x0, x1);
  193.     }
  194.  
  195.   return x;
  196. }
  197.  
  198. /* Returns the insn that next references REG after INSN, or 0
  199.    if REG is clobbered before next referenced or we cannot find
  200.    an insn that references REG in a straight-line piece of code.  */
  201.  
  202. rtx
  203. find_next_ref (reg, insn)
  204.      rtx reg;
  205.      rtx insn;
  206. {
  207.   rtx next;
  208.  
  209.   for (insn = NEXT_INSN (insn); insn; insn = next)
  210.     {
  211.       next = NEXT_INSN (insn);
  212.       if (GET_CODE (insn) == NOTE)
  213.     continue;
  214.       if (GET_CODE (insn) == CODE_LABEL
  215.       || GET_CODE (insn) == BARRIER)
  216.     return 0;
  217.       if (GET_CODE (insn) == INSN
  218.       || GET_CODE (insn) == JUMP_INSN
  219.       || GET_CODE (insn) == CALL_INSN)
  220.     {
  221.       if (reg_set_p (reg, insn))
  222.         return 0;
  223.       if (reg_mentioned_p (reg, PATTERN (insn)))
  224.         return insn;
  225.       if (GET_CODE (insn) == JUMP_INSN)
  226.         {
  227.           if (simplejump_p (insn))
  228.         next = JUMP_LABEL (insn);
  229.           else
  230.         return 0;
  231.         }
  232.       if (GET_CODE (insn) == CALL_INSN
  233.           && REGNO (reg) < FIRST_PSEUDO_REGISTER
  234.           && call_used_regs[REGNO (reg)])
  235.         return 0;
  236.     }
  237.       else
  238.     abort ();
  239.     }
  240.   return 0;
  241. }
  242.  
  243. /* Return an rtx for the size in bytes of the value of EXP.  */
  244.  
  245. rtx
  246. expr_size (exp)
  247.      tree exp;
  248. {
  249.   tree size = size_in_bytes (TREE_TYPE (exp));
  250.  
  251.   if (TREE_CODE (size) != INTEGER_CST
  252.       && contains_placeholder_p (size))
  253.     size = build (WITH_RECORD_EXPR, sizetype, size, exp);
  254.  
  255.   return expand_expr (size, NULL_RTX, TYPE_MODE (sizetype), 0);
  256. }
  257.  
  258. /* Return a copy of X in which all memory references
  259.    and all constants that involve symbol refs
  260.    have been replaced with new temporary registers.
  261.    Also emit code to load the memory locations and constants
  262.    into those registers.
  263.  
  264.    If X contains no such constants or memory references,
  265.    X itself (not a copy) is returned.
  266.  
  267.    If a constant is found in the address that is not a legitimate constant
  268.    in an insn, it is left alone in the hope that it might be valid in the
  269.    address.
  270.  
  271.    X may contain no arithmetic except addition, subtraction and multiplication.
  272.    Values returned by expand_expr with 1 for sum_ok fit this constraint.  */
  273.  
  274. static rtx
  275. break_out_memory_refs (x)
  276.      register rtx x;
  277. {
  278.   if (GET_CODE (x) == MEM
  279.       || (CONSTANT_P (x) && CONSTANT_ADDRESS_P (x)
  280.       && GET_MODE (x) != VOIDmode))
  281.     x = force_reg (GET_MODE (x), x);
  282.   else if (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
  283.        || GET_CODE (x) == MULT)
  284.     {
  285.       register rtx op0 = break_out_memory_refs (XEXP (x, 0));
  286.       register rtx op1 = break_out_memory_refs (XEXP (x, 1));
  287.  
  288.       if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
  289.     x = gen_rtx (GET_CODE (x), Pmode, op0, op1);
  290.     }
  291.  
  292.   return x;
  293. }
  294.  
  295. #ifdef POINTERS_EXTEND_UNSIGNED
  296.  
  297. /* Given X, a memory address in ptr_mode, convert it to an address
  298.    in Pmode, or vice versa (TO_MODE says which way).  We take advantage of
  299.    the fact that pointers are not allowed to overflow by commuting arithmetic
  300.    operations over conversions so that address arithmetic insns can be
  301.    used.  */
  302.  
  303. rtx
  304. convert_memory_address (to_mode, x)
  305.      enum machine_mode to_mode;
  306.      rtx x;
  307. {
  308.   rtx temp;
  309.  
  310.   switch (GET_CODE (x))
  311.     {
  312.     case CONST_INT:
  313.     case CONST_DOUBLE:
  314.       return x;
  315.  
  316.     case LABEL_REF:
  317.       return gen_rtx (LABEL_REF, to_mode, XEXP (x, 0));
  318.  
  319.     case SYMBOL_REF:
  320.       temp = gen_rtx (SYMBOL_REF, to_mode, XSTR (x, 0));
  321.       SYMBOL_REF_FLAG (temp) = SYMBOL_REF_FLAG (x);
  322.       return temp;
  323.  
  324.     case PLUS:
  325.     case MULT:
  326.       return gen_rtx (GET_CODE (x), to_mode, 
  327.               convert_memory_address (to_mode, XEXP (x, 0)),
  328.               convert_memory_address (to_mode, XEXP (x, 1)));
  329.  
  330.     case CONST:
  331.       return gen_rtx (CONST, to_mode, 
  332.               convert_memory_address (to_mode, XEXP (x, 0)));
  333.  
  334.     default:
  335.       return convert_modes (to_mode,
  336.                 to_mode == ptr_mode ? Pmode : ptr_mode,
  337.                 x, POINTERS_EXTEND_UNSIGNED);
  338.     }
  339. }
  340. #endif
  341.  
  342. /* Given a memory address or facsimile X, construct a new address,
  343.    currently equivalent, that is stable: future stores won't change it.
  344.  
  345.    X must be composed of constants, register and memory references
  346.    combined with addition, subtraction and multiplication:
  347.    in other words, just what you can get from expand_expr if sum_ok is 1.
  348.  
  349.    Works by making copies of all regs and memory locations used
  350.    by X and combining them the same way X does.
  351.    You could also stabilize the reference to this address
  352.    by copying the address to a register with copy_to_reg;
  353.    but then you wouldn't get indexed addressing in the reference.  */
  354.  
  355. rtx
  356. copy_all_regs (x)
  357.      register rtx x;
  358. {
  359.   if (GET_CODE (x) == REG)
  360.     {
  361.       if (REGNO (x) != FRAME_POINTER_REGNUM
  362. #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
  363.       && REGNO (x) != HARD_FRAME_POINTER_REGNUM
  364. #endif
  365.       )
  366.     x = copy_to_reg (x);
  367.     }
  368.   else if (GET_CODE (x) == MEM)
  369.     x = copy_to_reg (x);
  370.   else if (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
  371.        || GET_CODE (x) == MULT)
  372.     {
  373.       register rtx op0 = copy_all_regs (XEXP (x, 0));
  374.       register rtx op1 = copy_all_regs (XEXP (x, 1));
  375.       if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
  376.     x = gen_rtx (GET_CODE (x), Pmode, op0, op1);
  377.     }
  378.   return x;
  379. }
  380.  
  381. /* Return something equivalent to X but valid as a memory address
  382.    for something of mode MODE.  When X is not itself valid, this
  383.    works by copying X or subexpressions of it into registers.  */
  384.  
  385. rtx
  386. memory_address (mode, x)
  387.      enum machine_mode mode;
  388.      register rtx x;
  389. {
  390.   register rtx oldx = x;
  391.  
  392. #ifdef POINTERS_EXTEND_UNSIGNED
  393.   if (GET_MODE (x) == ptr_mode)
  394.     x = convert_memory_address (Pmode, x);
  395. #endif
  396.  
  397.   /* By passing constant addresses thru registers
  398.      we get a chance to cse them.  */
  399.   if (! cse_not_expected && CONSTANT_P (x) && CONSTANT_ADDRESS_P (x))
  400.     x = force_reg (Pmode, x);
  401.  
  402.   /* Accept a QUEUED that refers to a REG
  403.      even though that isn't a valid address.
  404.      On attempting to put this in an insn we will call protect_from_queue
  405.      which will turn it into a REG, which is valid.  */
  406.   else if (GET_CODE (x) == QUEUED
  407.       && GET_CODE (QUEUED_VAR (x)) == REG)
  408.     ;
  409.  
  410.   /* We get better cse by rejecting indirect addressing at this stage.
  411.      Let the combiner create indirect addresses where appropriate.
  412.      For now, generate the code so that the subexpressions useful to share
  413.      are visible.  But not if cse won't be done!  */
  414.   else
  415.     {
  416.       if (! cse_not_expected && GET_CODE (x) != REG)
  417.     x = break_out_memory_refs (x);
  418.  
  419.       /* At this point, any valid address is accepted.  */
  420.       GO_IF_LEGITIMATE_ADDRESS (mode, x, win);
  421.  
  422.       /* If it was valid before but breaking out memory refs invalidated it,
  423.      use it the old way.  */
  424.       if (memory_address_p (mode, oldx))
  425.     goto win2;
  426.  
  427.       /* Perform machine-dependent transformations on X
  428.      in certain cases.  This is not necessary since the code
  429.      below can handle all possible cases, but machine-dependent
  430.      transformations can make better code.  */
  431.       LEGITIMIZE_ADDRESS (x, oldx, mode, win);
  432.  
  433.       /* PLUS and MULT can appear in special ways
  434.      as the result of attempts to make an address usable for indexing.
  435.      Usually they are dealt with by calling force_operand, below.
  436.      But a sum containing constant terms is special
  437.      if removing them makes the sum a valid address:
  438.      then we generate that address in a register
  439.      and index off of it.  We do this because it often makes
  440.      shorter code, and because the addresses thus generated
  441.      in registers often become common subexpressions.  */
  442.       if (GET_CODE (x) == PLUS)
  443.     {
  444.       rtx constant_term = const0_rtx;
  445.       rtx y = eliminate_constant_term (x, &constant_term);
  446.       if (constant_term == const0_rtx
  447.           || ! memory_address_p (mode, y))
  448.         x = force_operand (x, NULL_RTX);
  449.       else
  450.         {
  451.           y = gen_rtx (PLUS, GET_MODE (x), copy_to_reg (y), constant_term);
  452.           if (! memory_address_p (mode, y))
  453.         x = force_operand (x, NULL_RTX);
  454.           else
  455.         x = y;
  456.         }
  457.     }
  458.  
  459.       else if (GET_CODE (x) == MULT || GET_CODE (x) == MINUS)
  460.     x = force_operand (x, NULL_RTX);
  461.  
  462.       /* If we have a register that's an invalid address,
  463.      it must be a hard reg of the wrong class.  Copy it to a pseudo.  */
  464.       else if (GET_CODE (x) == REG)
  465.     x = copy_to_reg (x);
  466.  
  467.       /* Last resort: copy the value to a register, since
  468.      the register is a valid address.  */
  469.       else
  470.     x = force_reg (Pmode, x);
  471.  
  472.       goto done;
  473.  
  474.     win2:
  475.       x = oldx;
  476.     win:
  477.       if (flag_force_addr && ! cse_not_expected && GET_CODE (x) != REG
  478.       /* Don't copy an addr via a reg if it is one of our stack slots.  */
  479.       && ! (GET_CODE (x) == PLUS
  480.         && (XEXP (x, 0) == virtual_stack_vars_rtx
  481.             || XEXP (x, 0) == virtual_incoming_args_rtx)))
  482.     {
  483.       if (general_operand (x, Pmode))
  484.         x = force_reg (Pmode, x);
  485.       else
  486.         x = force_operand (x, NULL_RTX);
  487.     }
  488.     }
  489.  
  490.  done:
  491.  
  492.   /* If we didn't change the address, we are done.  Otherwise, mark
  493.      a reg as a pointer if we have REG or REG + CONST_INT.  */
  494.   if (oldx == x)
  495.     return x;
  496.   else if (GET_CODE (x) == REG)
  497.     mark_reg_pointer (x);
  498.   else if (GET_CODE (x) == PLUS
  499.        && GET_CODE (XEXP (x, 0)) == REG
  500.        && GET_CODE (XEXP (x, 1)) == CONST_INT)
  501.     mark_reg_pointer (XEXP (x, 0));
  502.  
  503.   /* OLDX may have been the address on a temporary.  Update the address
  504.      to indicate that X is now used.  */
  505.   update_temp_slot_address (oldx, x);
  506.  
  507.   return x;
  508. }
  509.  
  510. /* Like `memory_address' but pretend `flag_force_addr' is 0.  */
  511.  
  512. rtx
  513. memory_address_noforce (mode, x)
  514.      enum machine_mode mode;
  515.      rtx x;
  516. {
  517.   int ambient_force_addr = flag_force_addr;
  518.   rtx val;
  519.  
  520.   flag_force_addr = 0;
  521.   val = memory_address (mode, x);
  522.   flag_force_addr = ambient_force_addr;
  523.   return val;
  524. }
  525.  
  526. /* Convert a mem ref into one with a valid memory address.
  527.    Pass through anything else unchanged.  */
  528.  
  529. rtx
  530. validize_mem (ref)
  531.      rtx ref;
  532. {
  533.   if (GET_CODE (ref) != MEM)
  534.     return ref;
  535.   if (memory_address_p (GET_MODE (ref), XEXP (ref, 0)))
  536.     return ref;
  537.   /* Don't alter REF itself, since that is probably a stack slot.  */
  538.   return change_address (ref, GET_MODE (ref), XEXP (ref, 0));
  539. }
  540.  
  541. /* Return a modified copy of X with its memory address copied
  542.    into a temporary register to protect it from side effects.
  543.    If X is not a MEM, it is returned unchanged (and not copied).
  544.    Perhaps even if it is a MEM, if there is no need to change it.  */
  545.  
  546. rtx
  547. stabilize (x)
  548.      rtx x;
  549. {
  550.   register rtx addr;
  551.   if (GET_CODE (x) != MEM)
  552.     return x;
  553.   addr = XEXP (x, 0);
  554.   if (rtx_unstable_p (addr))
  555.     {
  556.       rtx temp = copy_all_regs (addr);
  557.       rtx mem;
  558.       if (GET_CODE (temp) != REG)
  559.     temp = copy_to_reg (temp);
  560.       mem = gen_rtx (MEM, GET_MODE (x), temp);
  561.  
  562.       /* Mark returned memref with in_struct if it's in an array or
  563.      structure.  Copy const and volatile from original memref.  */
  564.  
  565.       MEM_IN_STRUCT_P (mem) = MEM_IN_STRUCT_P (x) || GET_CODE (addr) == PLUS;
  566.       RTX_UNCHANGING_P (mem) = RTX_UNCHANGING_P (x);
  567.       MEM_VOLATILE_P (mem) = MEM_VOLATILE_P (x);
  568.       return mem;
  569.     }
  570.   return x;
  571. }
  572.  
  573. /* Copy the value or contents of X to a new temp reg and return that reg.  */
  574.  
  575. rtx
  576. copy_to_reg (x)
  577.      rtx x;
  578. {
  579.   register rtx temp = gen_reg_rtx (GET_MODE (x));
  580.  
  581.   /* If not an operand, must be an address with PLUS and MULT so
  582.      do the computation.  */ 
  583.   if (! general_operand (x, VOIDmode))
  584.     x = force_operand (x, temp);
  585.   
  586.   if (x != temp)
  587.     emit_move_insn (temp, x);
  588.  
  589.   return temp;
  590. }
  591.  
  592. /* Like copy_to_reg but always give the new register mode Pmode
  593.    in case X is a constant.  */
  594.  
  595. rtx
  596. copy_addr_to_reg (x)
  597.      rtx x;
  598. {
  599.   return copy_to_mode_reg (Pmode, x);
  600. }
  601.  
  602. /* Like copy_to_reg but always give the new register mode MODE
  603.    in case X is a constant.  */
  604.  
  605. rtx
  606. copy_to_mode_reg (mode, x)
  607.      enum machine_mode mode;
  608.      rtx x;
  609. {
  610.   register rtx temp = gen_reg_rtx (mode);
  611.   
  612.   /* If not an operand, must be an address with PLUS and MULT so
  613.      do the computation.  */ 
  614.   if (! general_operand (x, VOIDmode))
  615.     x = force_operand (x, temp);
  616.  
  617.   if (GET_MODE (x) != mode && GET_MODE (x) != VOIDmode)
  618.     abort ();
  619.   if (x != temp)
  620.     emit_move_insn (temp, x);
  621.   return temp;
  622. }
  623.  
  624. /* Load X into a register if it is not already one.
  625.    Use mode MODE for the register.
  626.    X should be valid for mode MODE, but it may be a constant which
  627.    is valid for all integer modes; that's why caller must specify MODE.
  628.  
  629.    The caller must not alter the value in the register we return,
  630.    since we mark it as a "constant" register.  */
  631.  
  632. rtx
  633. force_reg (mode, x)
  634.      enum machine_mode mode;
  635.      rtx x;
  636. {
  637.   register rtx temp, insn, set;
  638.  
  639.   if (GET_CODE (x) == REG)
  640.     return x;
  641.   temp = gen_reg_rtx (mode);
  642.   insn = emit_move_insn (temp, x);
  643.  
  644.   /* Let optimizers know that TEMP's value never changes
  645.      and that X can be substituted for it.  Don't get confused
  646.      if INSN set something else (such as a SUBREG of TEMP).  */
  647.   if (CONSTANT_P (x)
  648.       && (set = single_set (insn)) != 0
  649.       && SET_DEST (set) == temp)
  650.     {
  651.       rtx note = find_reg_note (insn, REG_EQUAL, NULL_RTX);
  652.  
  653.       if (note)
  654.     XEXP (note, 0) = x;
  655.       else
  656.     REG_NOTES (insn) = gen_rtx (EXPR_LIST, REG_EQUAL, x, REG_NOTES (insn));
  657.     }
  658.   return temp;
  659. }
  660.  
  661. /* If X is a memory ref, copy its contents to a new temp reg and return
  662.    that reg.  Otherwise, return X.  */
  663.  
  664. rtx
  665. force_not_mem (x)
  666.      rtx x;
  667. {
  668.   register rtx temp;
  669.   if (GET_CODE (x) != MEM || GET_MODE (x) == BLKmode)
  670.     return x;
  671.   temp = gen_reg_rtx (GET_MODE (x));
  672.   emit_move_insn (temp, x);
  673.   return temp;
  674. }
  675.  
  676. /* Copy X to TARGET (if it's nonzero and a reg)
  677.    or to a new temp reg and return that reg.
  678.    MODE is the mode to use for X in case it is a constant.  */
  679.  
  680. rtx
  681. copy_to_suggested_reg (x, target, mode)
  682.      rtx x, target;
  683.      enum machine_mode mode;
  684. {
  685.   register rtx temp;
  686.  
  687.   if (target && GET_CODE (target) == REG)
  688.     temp = target;
  689.   else
  690.     temp = gen_reg_rtx (mode);
  691.  
  692.   emit_move_insn (temp, x);
  693.   return temp;
  694. }
  695.  
  696. /* Return the mode to use to store a scalar of TYPE and MODE.
  697.    PUNSIGNEDP points to the signedness of the type and may be adjusted
  698.    to show what signedness to use on extension operations.
  699.  
  700.    FOR_CALL is non-zero if this call is promoting args for a call.  */
  701.  
  702. enum machine_mode
  703. promote_mode (type, mode, punsignedp, for_call)
  704.      tree type;
  705.      enum machine_mode mode;
  706.      int *punsignedp;
  707.      int for_call;
  708. {
  709.   enum tree_code code = TREE_CODE (type);
  710.   int unsignedp = *punsignedp;
  711.  
  712. #ifdef PROMOTE_FOR_CALL_ONLY
  713.   if (! for_call)
  714.     return mode;
  715. #endif
  716.  
  717.   switch (code)
  718.     {
  719. #ifdef PROMOTE_MODE
  720.     case INTEGER_TYPE:   case ENUMERAL_TYPE:   case BOOLEAN_TYPE:
  721.     case CHAR_TYPE:      case REAL_TYPE:       case OFFSET_TYPE:
  722.       PROMOTE_MODE (mode, unsignedp, type);
  723.       break;
  724. #endif
  725.  
  726. #ifdef POINTERS_EXTEND_UNSIGNED
  727.     case POINTER_TYPE:
  728.       mode = Pmode;
  729.       unsignedp = POINTERS_EXTEND_UNSIGNED;
  730.       break;
  731. #endif
  732.     }
  733.  
  734.   *punsignedp = unsignedp;
  735.   return mode;
  736. }
  737.  
  738. /* Adjust the stack pointer by ADJUST (an rtx for a number of bytes).
  739.    This pops when ADJUST is positive.  ADJUST need not be constant.  */
  740.  
  741. void
  742. adjust_stack (adjust)
  743.      rtx adjust;
  744. {
  745.   rtx temp;
  746.   adjust = protect_from_queue (adjust, 0);
  747.  
  748.   if (adjust == const0_rtx)
  749.     return;
  750.  
  751.   temp = expand_binop (Pmode,
  752. #ifdef STACK_GROWS_DOWNWARD
  753.                add_optab,
  754. #else
  755.                sub_optab,
  756. #endif
  757.                stack_pointer_rtx, adjust, stack_pointer_rtx, 0,
  758.                OPTAB_LIB_WIDEN);
  759.  
  760.   if (temp != stack_pointer_rtx)
  761.     emit_move_insn (stack_pointer_rtx, temp);
  762. }
  763.  
  764. /* Adjust the stack pointer by minus ADJUST (an rtx for a number of bytes).
  765.    This pushes when ADJUST is positive.  ADJUST need not be constant.  */
  766.  
  767. void
  768. anti_adjust_stack (adjust)
  769.      rtx adjust;
  770. {
  771.   rtx temp;
  772.   adjust = protect_from_queue (adjust, 0);
  773.  
  774.   if (adjust == const0_rtx)
  775.     return;
  776.  
  777.   temp = expand_binop (Pmode,
  778. #ifdef STACK_GROWS_DOWNWARD
  779.                sub_optab,
  780. #else
  781.                add_optab,
  782. #endif
  783.                stack_pointer_rtx, adjust, stack_pointer_rtx, 0,
  784.                OPTAB_LIB_WIDEN);
  785.  
  786.   if (temp != stack_pointer_rtx)
  787.     emit_move_insn (stack_pointer_rtx, temp);
  788. }
  789.  
  790. /* Round the size of a block to be pushed up to the boundary required
  791.    by this machine.  SIZE is the desired size, which need not be constant.  */
  792.  
  793. rtx
  794. round_push (size)
  795.      rtx size;
  796. {
  797. #ifdef STACK_BOUNDARY
  798.   int align = STACK_BOUNDARY / BITS_PER_UNIT;
  799.   if (align == 1)
  800.     return size;
  801.   if (GET_CODE (size) == CONST_INT)
  802.     {
  803.       int new = (INTVAL (size) + align - 1) / align * align;
  804.       if (INTVAL (size) != new)
  805.     size = GEN_INT (new);
  806.     }
  807.   else
  808.     {
  809.       /* CEIL_DIV_EXPR needs to worry about the addition overflowing,
  810.      but we know it can't.  So add ourselves and then do TRUNC_DIV_EXPR. */
  811.       size = expand_binop (Pmode, add_optab, size, GEN_INT (align - 1),
  812.                NULL_RTX, 1, OPTAB_LIB_WIDEN);
  813.       size = expand_divmod (0, TRUNC_DIV_EXPR, Pmode, size, GEN_INT (align),
  814.                 NULL_RTX, 1);
  815.       size = expand_mult (Pmode, size, GEN_INT (align), NULL_RTX, 1);
  816.     }
  817. #endif /* STACK_BOUNDARY */
  818.   return size;
  819. }
  820.  
  821. /* Save the stack pointer for the purpose in SAVE_LEVEL.  PSAVE is a pointer
  822.    to a previously-created save area.  If no save area has been allocated,
  823.    this function will allocate one.  If a save area is specified, it
  824.    must be of the proper mode.
  825.  
  826.    The insns are emitted after insn AFTER, if nonzero, otherwise the insns
  827.    are emitted at the current position.  */
  828.  
  829. void
  830. emit_stack_save (save_level, psave, after)
  831.      enum save_level save_level;
  832.      rtx *psave;
  833.      rtx after;
  834. {
  835.   rtx sa = *psave;
  836.   /* The default is that we use a move insn and save in a Pmode object.  */
  837.   rtx (*fcn) () = gen_move_insn;
  838.   enum machine_mode mode = Pmode;
  839.  
  840.   /* See if this machine has anything special to do for this kind of save.  */
  841.   switch (save_level)
  842.     {
  843. #ifdef HAVE_save_stack_block
  844.     case SAVE_BLOCK:
  845.       if (HAVE_save_stack_block)
  846.     {
  847.       fcn = gen_save_stack_block;
  848.       mode = insn_operand_mode[CODE_FOR_save_stack_block][0];
  849.     }
  850.       break;
  851. #endif
  852. #ifdef HAVE_save_stack_function
  853.     case SAVE_FUNCTION:
  854.       if (HAVE_save_stack_function)
  855.     {
  856.       fcn = gen_save_stack_function;
  857.       mode = insn_operand_mode[CODE_FOR_save_stack_function][0];
  858.     }
  859.       break;
  860. #endif
  861. #ifdef HAVE_save_stack_nonlocal
  862.     case SAVE_NONLOCAL:
  863.       if (HAVE_save_stack_nonlocal)
  864.     {
  865.       fcn = gen_save_stack_nonlocal;
  866.       mode = insn_operand_mode[(int) CODE_FOR_save_stack_nonlocal][0];
  867.     }
  868.       break;
  869. #endif
  870.     }
  871.  
  872.   /* If there is no save area and we have to allocate one, do so.  Otherwise
  873.      verify the save area is the proper mode.  */
  874.  
  875.   if (sa == 0)
  876.     {
  877.       if (mode != VOIDmode)
  878.     {
  879.       if (save_level == SAVE_NONLOCAL)
  880.         *psave = sa = assign_stack_local (mode, GET_MODE_SIZE (mode), 0);
  881.       else
  882.         *psave = sa = gen_reg_rtx (mode);
  883.     }
  884.     }
  885.   else
  886.     {
  887.       if (mode == VOIDmode || GET_MODE (sa) != mode)
  888.     abort ();
  889.     }
  890.  
  891.   if (after)
  892.     {
  893.       rtx seq;
  894.  
  895.       start_sequence ();
  896.       /* We must validize inside the sequence, to ensure that any instructions
  897.      created by the validize call also get moved to the right place.  */
  898.       if (sa != 0)
  899.     sa = validize_mem (sa);
  900.       emit_insn (fcn (sa, stack_pointer_rtx));
  901.       seq = gen_sequence ();
  902.       end_sequence ();
  903.       emit_insn_after (seq, after);
  904.     }
  905.   else
  906.     {
  907.       if (sa != 0)
  908.     sa = validize_mem (sa);
  909.       emit_insn (fcn (sa, stack_pointer_rtx));
  910.     }
  911. }
  912.  
  913. /* Restore the stack pointer for the purpose in SAVE_LEVEL.  SA is the save
  914.    area made by emit_stack_save.  If it is zero, we have nothing to do. 
  915.  
  916.    Put any emitted insns after insn AFTER, if nonzero, otherwise at 
  917.    current position.  */
  918.  
  919. void
  920. emit_stack_restore (save_level, sa, after)
  921.      enum save_level save_level;
  922.      rtx after;
  923.      rtx sa;
  924. {
  925.   /* The default is that we use a move insn.  */
  926.   rtx (*fcn) () = gen_move_insn;
  927.  
  928.   /* See if this machine has anything special to do for this kind of save.  */
  929.   switch (save_level)
  930.     {
  931. #ifdef HAVE_restore_stack_block
  932.     case SAVE_BLOCK:
  933.       if (HAVE_restore_stack_block)
  934.     fcn = gen_restore_stack_block;
  935.       break;
  936. #endif
  937. #ifdef HAVE_restore_stack_function
  938.     case SAVE_FUNCTION:
  939.       if (HAVE_restore_stack_function)
  940.     fcn = gen_restore_stack_function;
  941.       break;
  942. #endif
  943. #ifdef HAVE_restore_stack_nonlocal
  944.  
  945.     case SAVE_NONLOCAL:
  946.       if (HAVE_restore_stack_nonlocal)
  947.     fcn = gen_restore_stack_nonlocal;
  948.       break;
  949. #endif
  950.     }
  951.  
  952.   if (sa != 0)
  953.     sa = validize_mem (sa);
  954.  
  955.   if (after)
  956.     {
  957.       rtx seq;
  958.  
  959.       start_sequence ();
  960.       emit_insn (fcn (stack_pointer_rtx, sa));
  961.       seq = gen_sequence ();
  962.       end_sequence ();
  963.       emit_insn_after (seq, after);
  964.     }
  965.   else
  966.     emit_insn (fcn (stack_pointer_rtx, sa));
  967. }
  968.  
  969. /* Return an rtx representing the address of an area of memory dynamically
  970.    pushed on the stack.  This region of memory is always aligned to
  971.    a multiple of BIGGEST_ALIGNMENT.
  972.  
  973.    Any required stack pointer alignment is preserved.
  974.  
  975.    SIZE is an rtx representing the size of the area.
  976.    TARGET is a place in which the address can be placed.
  977.  
  978.    KNOWN_ALIGN is the alignment (in bits) that we know SIZE has.  */
  979.  
  980. rtx
  981. allocate_dynamic_stack_space (size, target, known_align)
  982.      rtx size;
  983.      rtx target;
  984.      int known_align;
  985. {
  986.   /* If we're asking for zero bytes, it doesn't matter what we point
  987.      to since we can't dereference it.  But return a reasonable
  988.      address anyway.  */
  989.   if (size == const0_rtx)
  990.     return virtual_stack_dynamic_rtx;
  991.  
  992.   /* Otherwise, show we're calling alloca or equivalent.  */
  993.   current_function_calls_alloca = 1;
  994.  
  995.   /* Ensure the size is in the proper mode.  */
  996.   if (GET_MODE (size) != VOIDmode && GET_MODE (size) != Pmode)
  997.     size = convert_to_mode (Pmode, size, 1);
  998.  
  999.   /* We will need to ensure that the address we return is aligned to
  1000.      BIGGEST_ALIGNMENT.  If STACK_DYNAMIC_OFFSET is defined, we don't
  1001.      always know its final value at this point in the compilation (it 
  1002.      might depend on the size of the outgoing parameter lists, for
  1003.      example), so we must align the value to be returned in that case.
  1004.      (Note that STACK_DYNAMIC_OFFSET will have a default non-zero value if
  1005.      STACK_POINTER_OFFSET or ACCUMULATE_OUTGOING_ARGS are defined).
  1006.      We must also do an alignment operation on the returned value if
  1007.      the stack pointer alignment is less strict that BIGGEST_ALIGNMENT.
  1008.  
  1009.      If we have to align, we must leave space in SIZE for the hole
  1010.      that might result from the alignment operation.  */
  1011.  
  1012. #if defined (STACK_DYNAMIC_OFFSET) || defined (STACK_POINTER_OFFSET) || defined (ALLOCATE_OUTGOING_ARGS) || ! defined (STACK_BOUNDARY)
  1013. #define MUST_ALIGN 1
  1014. #else
  1015. #define MUST_ALIGN (STACK_BOUNDARY < BIGGEST_ALIGNMENT)
  1016. #endif
  1017.  
  1018.   if (MUST_ALIGN)
  1019.     {
  1020.       if (GET_CODE (size) == CONST_INT)
  1021.     size = GEN_INT (INTVAL (size)
  1022.             + (BIGGEST_ALIGNMENT / BITS_PER_UNIT - 1));
  1023.       else
  1024.     size = expand_binop (Pmode, add_optab, size,
  1025.                  GEN_INT (BIGGEST_ALIGNMENT / BITS_PER_UNIT - 1),
  1026.                  NULL_RTX, 1, OPTAB_LIB_WIDEN);
  1027.     }
  1028.  
  1029. #ifdef SETJMP_VIA_SAVE_AREA
  1030.   /* If setjmp restores regs from a save area in the stack frame,
  1031.      avoid clobbering the reg save area.  Note that the offset of
  1032.      virtual_incoming_args_rtx includes the preallocated stack args space.
  1033.      It would be no problem to clobber that, but it's on the wrong side
  1034.      of the old save area.  */
  1035.   {
  1036.     rtx dynamic_offset
  1037.       = expand_binop (Pmode, sub_optab, virtual_stack_dynamic_rtx,
  1038.               stack_pointer_rtx, NULL_RTX, 1, OPTAB_LIB_WIDEN);
  1039.     size = expand_binop (Pmode, add_optab, size, dynamic_offset,
  1040.              NULL_RTX, 1, OPTAB_LIB_WIDEN);
  1041.   }
  1042. #endif /* SETJMP_VIA_SAVE_AREA */
  1043.  
  1044.   /* Round the size to a multiple of the required stack alignment.
  1045.      Since the stack if presumed to be rounded before this allocation,
  1046.      this will maintain the required alignment.
  1047.  
  1048.      If the stack grows downward, we could save an insn by subtracting
  1049.      SIZE from the stack pointer and then aligning the stack pointer.
  1050.      The problem with this is that the stack pointer may be unaligned
  1051.      between the execution of the subtraction and alignment insns and
  1052.      some machines do not allow this.  Even on those that do, some
  1053.      signal handlers malfunction if a signal should occur between those
  1054.      insns.  Since this is an extremely rare event, we have no reliable
  1055.      way of knowing which systems have this problem.  So we avoid even
  1056.      momentarily mis-aligning the stack.  */
  1057.  
  1058. #ifdef STACK_BOUNDARY
  1059.   /* If we added a variable amount to SIZE,
  1060.      we can no longer assume it is aligned.  */
  1061. #if !defined (SETJMP_VIA_SAVE_AREA)
  1062.   if (MUST_ALIGN || known_align % STACK_BOUNDARY != 0)
  1063. #endif
  1064.     size = round_push (size);
  1065. #endif
  1066.  
  1067.   do_pending_stack_adjust ();
  1068.  
  1069.   /* Don't use a TARGET that isn't a pseudo.  */
  1070.   if (target == 0 || GET_CODE (target) != REG
  1071.       || REGNO (target) < FIRST_PSEUDO_REGISTER)
  1072.     target = gen_reg_rtx (Pmode);
  1073.  
  1074.   mark_reg_pointer (target);
  1075.  
  1076. #ifndef STACK_GROWS_DOWNWARD
  1077.   emit_move_insn (target, virtual_stack_dynamic_rtx);
  1078. #endif
  1079.  
  1080.   /* Perform the required allocation from the stack.  Some systems do
  1081.      this differently than simply incrementing/decrementing from the
  1082.      stack pointer.  */
  1083. #ifdef HAVE_allocate_stack
  1084.   if (HAVE_allocate_stack)
  1085.     {
  1086.       enum machine_mode mode
  1087.     = insn_operand_mode[(int) CODE_FOR_allocate_stack][0];
  1088.  
  1089.       size = convert_modes (mode, ptr_mode, size, 1);
  1090.  
  1091.       if (insn_operand_predicate[(int) CODE_FOR_allocate_stack][0]
  1092.       && ! ((*insn_operand_predicate[(int) CODE_FOR_allocate_stack][0])
  1093.         (size, mode)))
  1094.     size = copy_to_mode_reg (mode, size);
  1095.  
  1096.       emit_insn (gen_allocate_stack (size));
  1097.     }
  1098.   else
  1099. #endif
  1100. #if 1 /* HAVE_stack_on_heap */
  1101.   if (TARGET_STACKCHECK)
  1102.     emit_insn (gen_stack_management_call
  1103.                 (stack_pointer_rtx, size, "jbsr ___stkchk_d0"));
  1104.   if (TARGET_STACKEXTEND)
  1105.     emit_insn (gen_stack_management_call 
  1106.                 (stack_pointer_rtx, size, "jbsr ___sub_d0_sp"));
  1107.   else
  1108. #endif
  1109.     {
  1110.       size = convert_modes (Pmode, ptr_mode, size, 1);
  1111.       anti_adjust_stack (size);
  1112.     }
  1113.  
  1114. #ifdef STACK_GROWS_DOWNWARD
  1115.   emit_move_insn (target, virtual_stack_dynamic_rtx);
  1116. #endif
  1117.  
  1118.   if (MUST_ALIGN)
  1119.     {
  1120.       /* CEIL_DIV_EXPR needs to worry about the addition overflowing,
  1121.      but we know it can't.  So add ourselves and then do TRUNC_DIV_EXPR. */
  1122.       target = expand_binop (Pmode, add_optab, target,
  1123.                  GEN_INT (BIGGEST_ALIGNMENT / BITS_PER_UNIT - 1),
  1124.                  NULL_RTX, 1, OPTAB_LIB_WIDEN);
  1125.       target = expand_divmod (0, TRUNC_DIV_EXPR, Pmode, target,
  1126.                   GEN_INT (BIGGEST_ALIGNMENT / BITS_PER_UNIT),
  1127.                   NULL_RTX, 1);
  1128.       target = expand_mult (Pmode, target,
  1129.                 GEN_INT (BIGGEST_ALIGNMENT / BITS_PER_UNIT),
  1130.                 NULL_RTX, 1);
  1131.     }
  1132.   
  1133.   /* Some systems require a particular insn to refer to the stack
  1134.      to make the pages exist.  */
  1135. #ifdef HAVE_probe
  1136.   if (HAVE_probe)
  1137.     emit_insn (gen_probe ());
  1138. #endif
  1139.  
  1140.   /* Record the new stack level for nonlocal gotos.  */
  1141.   if (nonlocal_goto_handler_slot != 0)
  1142.     emit_stack_save (SAVE_NONLOCAL, &nonlocal_goto_stack_level, NULL_RTX);
  1143.  
  1144.   return target;
  1145. }
  1146.  
  1147. /* Return an rtx representing the register or memory location
  1148.    in which a scalar value of data type VALTYPE
  1149.    was returned by a function call to function FUNC.
  1150.    FUNC is a FUNCTION_DECL node if the precise function is known,
  1151.    otherwise 0.  */
  1152.  
  1153. rtx
  1154. hard_function_value (valtype, func)
  1155.      tree valtype;
  1156.      tree func;
  1157. {
  1158.   rtx val = FUNCTION_VALUE (valtype, func);
  1159.   if (GET_CODE (val) == REG
  1160.       && GET_MODE (val) == BLKmode)
  1161.     {
  1162.       int bytes = int_size_in_bytes (valtype);
  1163.       enum machine_mode tmpmode;
  1164.       for (tmpmode = GET_CLASS_NARROWEST_MODE (MODE_INT);
  1165.            tmpmode != MAX_MACHINE_MODE;
  1166.            tmpmode = GET_MODE_WIDER_MODE (tmpmode))
  1167.         {
  1168.           /* Have we found a large enough mode?  */
  1169.           if (GET_MODE_SIZE (tmpmode) >= bytes)
  1170.             break;
  1171.         }
  1172.  
  1173.       /* No suitable mode found.  */
  1174.       if (tmpmode == MAX_MACHINE_MODE)
  1175.         abort ();
  1176.  
  1177.       PUT_MODE (val, tmpmode);
  1178.     }      
  1179.   return val;
  1180. }
  1181.  
  1182. /* Return an rtx representing the register or memory location
  1183.    in which a scalar value of mode MODE was returned by a library call.  */
  1184.  
  1185. rtx
  1186. hard_libcall_value (mode)
  1187.      enum machine_mode mode;
  1188. {
  1189.   return LIBCALL_VALUE (mode);
  1190. }
  1191.  
  1192. /* Look up the tree code for a given rtx code
  1193.    to provide the arithmetic operation for REAL_ARITHMETIC.
  1194.    The function returns an int because the caller may not know
  1195.    what `enum tree_code' means.  */
  1196.  
  1197. int
  1198. rtx_to_tree_code (code)
  1199.      enum rtx_code code;
  1200. {
  1201.   enum tree_code tcode;
  1202.  
  1203.   switch (code)
  1204.     {
  1205.     case PLUS:
  1206.       tcode = PLUS_EXPR;
  1207.       break;
  1208.     case MINUS:
  1209.       tcode = MINUS_EXPR;
  1210.       break;
  1211.     case MULT:
  1212.       tcode = MULT_EXPR;
  1213.       break;
  1214.     case DIV:
  1215.       tcode = RDIV_EXPR;
  1216.       break;
  1217.     case SMIN:
  1218.       tcode = MIN_EXPR;
  1219.       break;
  1220.     case SMAX:
  1221.       tcode = MAX_EXPR;
  1222.       break;
  1223.     default:
  1224.       tcode = LAST_AND_UNUSED_TREE_CODE;
  1225.       break;
  1226.     }
  1227.   return ((int) tcode);
  1228. }
  1229.